package cn.edu.neusoft.food729.model;


import java.util.List;

import cn.edu.neusoft.food729.Bean.OrdersBean;
import cn.edu.neusoft.food729.common.Common;
import cn.edu.neusoft.food729.iface.Ordersiface;
import cn.edu.neusoft.food729.listener.OrdersListener;
import cn.edu.neusoft.food729.service.GetAllOrdersByUserService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class OrdersModel implements Ordersiface {

    private Retrofit retrofit;

    public OrdersModel() {
        retrofit = new Retrofit.Builder()
                .baseUrl(Common.BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    @Override
    public void getOrdersResult(int user_id, final OrdersListener ordersListener) {
        GetAllOrdersByUserService getAllOrdersByUserService
                = retrofit.create(GetAllOrdersByUserService.class);
        Call<List<OrdersBean>> call = getAllOrdersByUserService.orders(user_id);

        call.enqueue(new Callback<List<OrdersBean>>() {
            @Override
            public void onResponse(Call<List<OrdersBean>> call, Response<List<OrdersBean>> response) {
                if (response.isSuccessful() && response.body() != null)
                    ordersListener.onResponse(response.body());
                else ordersListener.onFail("on response fail");
            }

            @Override
            public void onFailure(Call<List<OrdersBean>> call, Throwable t) {
                System.out.println("--网络错误：" + t.getMessage());
                ordersListener.onFail(t.getMessage());
            }
        });
    }
}
